package cn.shequren.communityPeople.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.jz.community.basecomm.constant.AppConstants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import cn.shequren.communityPeople.pay.utils.WXPayUtil;

import cn.shequren.communityPeople.pay.constants.CommUnityPayConstants;

/**
 * Created by Tony.Fan on 2018/3/16 10:11
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, AppConstants.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Intent intent = new Intent();
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            intent.setAction(CommUnityPayConstants.WEIXIN_PAY_INTENT_ACTION);
            if (baseResp.errCode == 0) {
                intent.putExtra("payResult", CommUnityPayConstants.PAY_RESULT_SUCCESS);
            } else if (baseResp.errCode == -2) {
                intent.putExtra("payResult", CommUnityPayConstants.PAY_RESULT_CANCEL);
            } else {
                intent.putExtra("payResult", CommUnityPayConstants.PAY_RESULT_ERROR);
            }
        }
        sendBroadcast(intent);
        finish();
    }
}
